/**
 * @BelongsProject: test
 * @BelongsPackage: com.hmall.user.service
 * @Author: 王明天
 * @CreateTime: 2025/2/19 10:26
 * @Description: UserServiceImpl
 */
package com.hmall.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.api.user.userDto.User;
import com.hmall.user.config.JwtProperties;
import com.hmall.user.domain.dto.LoginDto;
import com.hmall.user.domain.vo.UserLoginVO;
import com.hmall.user.exception.CommonException;
import com.hmall.user.mapper.UserMapper;
import com.hmall.user.service.IUserService;
import com.hmall.user.utils.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final JwtTool jwtTool;

    private final JwtProperties jwtProperties;

    private final UserMapper userMapper;


    @Override
    public UserLoginVO login(LoginDto loginDTO) {

        // 1.数据校验
        String mobile = loginDTO.getMobile();
        String password = loginDTO.getPwd();
        if (ObjectUtil.isEmpty(mobile) || ObjectUtil.isEmpty(password)) {

            Assert.notNull(mobile, "手机号或密码不能为空");
        }

        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(User::getMobile, mobile);
        User user = getOne(queryWrapper);

        if (ObjectUtil.isEmpty(user)) {
            throw new CommonException("手机号或密码错误", 401);
        }

        if (!user.getPwd().equals(password)) {
            throw new CommonException("手机号或密码错误", 401);
        }


        // 3.校验是否禁用
        if (user.getStatus() == 1) {
            throw new CommonException("该用户被禁用", 401);
        }

        if (user.getStatus() == 2) {
            throw new CommonException("该用户被锁定", 401);
        }

        // 5.生成TOKEN
        String token = jwtTool.createToken(user.getId(), jwtProperties.getTokenTTL());
        // 6.封装VO返回
        UserLoginVO vo = new UserLoginVO();
        vo.setUserId(user.getId());
        vo.setUsername(user.getName());
        vo.setToken(token);
        return vo;
    }

    /**
     * 新增用户
     *
     * @param user
     */
    @Override
    public void saveUser(User user) {
        user.setStatus(0);
        user.setCreatedTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        save(user);
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public User getUserById(Long id) {

        return getById(id);
    }

    /**
     * 根据id删除用户
     *
     * @param id
     */
    @Override
    public void removeUserById(Long id) {
        removeById(id);
    }


    /**
     * 更新用户
     *
     * @param user
     */
    @Override
    public void updateUser(User user) {


        user.setUpdateTime(LocalDateTime.now());
        updateById(user);

    }

    /**
     * 根据ids批量查询用户
     *
     * @param ids
     * @return
     */
    @Override
    public List<User> getUserByIds(Long[] ids) {
        List<User> list = new ArrayList<>();
        Arrays.stream(ids).forEach(id->{
            list.add(getById(id));
        });
        return list;

    }
}
